package com.shuqi.common;

import android.app.Activity;
import android.content.Context;

import com.shuqi.activity.LoginActivity;
import com.shuqi.activity.viewport.DialogLoading;
import com.shuqi.application.ShuqiApplication;
import com.shuqi.common.utils.Log4an;
import com.shuqi.database.model.UserInfo;
import com.shuqi.interfaces.CheckUserListener;
import com.shuqi.interfaces.WatingListener;
import com.shuqi.model.manager.MyAccountManager;

public class CheckUser {
    /**验证身份失败：跳转到登录页，requestcode*/
    public static final int REQUESTCODE_JUMPLOGIN = 300;
    
    private static final String logTag = "CheckUser";
    
    /**
     * 所有需要验证用户的地方，检查用户状态
     * @param activity
     * @param checkListener
     */
    public static void checkUserStateBeforeJump(final Activity activity, final CheckUserListener checkListener){
        final DialogLoading dialog = new DialogLoading(activity);
        
        Log4an.i(logTag, "checkUserStateBeforeJump from:"+(activity.getClass().getSimpleName()));
        WatingListener myListener = new WatingListener() {
            @Override
            public void onBeforeNetWorking() {
                Log4an.i(logTag, "开始联网验证...显示对话框");
                dialog.show();
            }
            @Override
            public void onFinish(final boolean isSuccess) {
                Runnable r = new Runnable() {
                    @Override
                    public void run() {
                        if(dialog.isShowing()){
                            dialog.dismiss();
                        }
                        UserInfo userInfo = MyAccountManager.getInstance(activity, true);
                        Log4an.i(logTag, "OnFinish: "+isSuccess+",userId="+userInfo.getUserId()+",userState="+userInfo.getUserState());
                        if(!MyAccountManager.isNeedCheckUserState(userInfo)){
                            if(checkListener!=null){
                                checkListener.onPassed();
                            }else{
                                //TODO 
                            }
                        }else{
                            LoginActivity.openLoginForResult(activity,REQUESTCODE_JUMPLOGIN);
//                            ActivityManager.getInstance().finishActivity(activity);
                        }
                    }
                };
                ShuqiApplication.getApplicationHandler().post(r);
            }

        };
        checkUserState(activity.getApplicationContext(), myListener);
    }
    
    private static void checkUserState(final Context context, final WatingListener watingListener) {
        UserInfo mUser = MyAccountManager.getInstance(context,true);
        if (MyAccountManager.isGuestUser(mUser)) {
            watingListener.onBeforeNetWorking();
            MyAccountManager.getUserInfoOnline(context, watingListener);//游客，获取用户身份
        }else if(MyAccountManager.isCommonUser(mUser)){
            if(MyAccountManager.isNeedCheckUserState(mUser)){
              //准会员：未验证
                watingListener.onBeforeNetWorking();
                MyAccountManager.checkUserStateOnline(context, watingListener);
            }else{
              //准会员：已验证  
                watingListener.onFinish(true);
            }
        }else if(MyAccountManager.isVipUser(mUser)){
            if(MyAccountManager.isNeedCheckUserState(mUser)){
                //会员：未验证
                watingListener.onFinish(false);
            }else{
              //会员：已验证
                watingListener.onFinish(true);
            }
        }else{
            //错误的用户身份
            watingListener.onFinish(false);
        }
    }

}
